# 人生苦短，我用python
# @Time : 2021/11/18 11:27
# @Author : shiChao
# @File : loadObjectWithInput.py
# @Desc : 加载出所有的用例并且展示在界面上, 后续加上多模块，多用例


from colorama import init, Fore
from loadData import loadData
import os
init(autoreset=True)


class LoadDataWithInput:

    def __init__(self):
        self.data = loadData.getData()
        self.checkData()
        print(Fore.GREEN + '只支持单模块运行')
        print('请选择要执行的用例')

        self.outPutCase()

    def setup_class(self) -> None:
        pass

    @staticmethod
    def read():
        return input(">>>>>>>>>>>>>>>>请输入要执行的用例/模块:")

    # 检查用例是否存在
    def handleCase(self, caseName: str):
        # 空
        if len(caseName) == 0:
            exit()

        allCase = caseName.split(',')

        # 笨办法，通过截取后两位来判断是否为模块
        res = []
        for i in allCase:
            caseType = i[-2:]
            if caseType == '模块':
                res = self.checkModule(i)
            else:
                res = []

        # 检查获取到的用例是否存在
        self.checkFile(res)

    # 判断模块是否存在
    def checkModule(self, caseName: str):
        caseFile = []
        if caseName in self.data:
            for j in self.data[caseName]:
                file = self.data[caseName][j].get('path') + self.data[caseName][j].get('object_name')
                caseFile.append(file)

        return caseFile

    # 检查用例文件是否存在
    @staticmethod
    def checkFile(caseFile: list):
        if not caseFile:
            print(Fore.RED + '>>>>>>>>>>>>>>>>配置查询错误')

        for i in caseFile:
            if not os.path.exists(i):
                print(Fore.YELLOW + '>>>>>>>>>>>>>>>>【' + i + '】文件不存在,请调整配置')

    # 用例处理，以描述对应文件名的形式展示
    def outPutCase(self):
        headSpace = ' '
        data = self.data

        for i in data:
            if len(i) <= 10:
                num = 10 - len(i)
                note = i
            else:
                num = 0
                note = i

            # 目录展示
            DIR_LEFT_SPACE = headSpace.ljust(17)
            inputLine = Fore.RED + DIR_LEFT_SPACE + note.ljust(num + 20)
            print(inputLine)

            # 具体用例展示
            for j in data[i]:
                if len(j) <= 10:
                    num = 10 - len(j)
                else:
                    num = len(j) - 10

                # 左侧占位
                LEFT_SPACE = headSpace.ljust(27)

                # 用例ID，用于执行单个用例，或者不同用例目录执行
                CASE_ID = str(data[i][j].get('id')) + ' '

                # 用例描述
                NOTE_DESC = j.ljust(num + 15)

                # 用例文件名
                NOTE_FILE = data[i][j].get('object_name')

                inputLine = LEFT_SPACE + CASE_ID + NOTE_DESC + NOTE_FILE
                print(inputLine)

    # 数据检查，只检查数据是否必填
    def checkData(self):
        data = self.data
        for i in data:
            for j in data[i]:
                # 判断用例ID、名称、路径
                if not data[i][j].get('id') or not data[i][j].get('object_name') or not data[i][j].get('path'):
                    print(Fore.RED + '数据源错误，请保证用例ID，名称，路径全部填写')
                    print(Fore.RED + '>>>>>>>>>>>>>>>>END')
                    exit()


case = LoadDataWithInput()
inputCase = case.read()
case.handleCase(inputCase)
